package com.panshi.vo.comm;

import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TimeList<T> {
    private T t;

    /**
     * List集合时间转换方法
     * @param lsit
     * @param getNames
     * @return
     */
    public List<T> switchTimeList(List<T> lsit,String[] getNames){
        for (T objT:lsit) {
            Class clazz = objT.getClass();

            for (String getName:getNames) {
                try {
                    Method method = clazz.getMethod(getName);
                    //获取时间
                    String strTime = (String) method.invoke(objT);

                    //获取set方法
                    String setName = ("set"+getName.split("get")[1]);
                    Method m = clazz.getMethod(setName,String.class);
                    //获取当前时间差
                    String str = getDatePoor(strTime);
                    //System.out.println(str);
                    //给t1对象赋值
                    m.invoke(objT,str);
                }catch (Exception e){
                    System.out.println("时间转换异常-------------------------");
                }
            }

        }
        System.out.println(lsit);
        return lsit;
    }

    /**
     * 对象时间转换方法
     * @param objT
     * @param getNames
     * @return
     */
    public T switchTime(T objT,String[] getNames) {

        Class clazz = objT.getClass();

        for (String getName:getNames) {
           try {
               Method method = clazz.getMethod(getName);
               //获取时间
               String strTime = (String) method.invoke(objT);

               //获取set方法
               String setName = ("set"+getName.split("get")[1]);
               Method m = clazz.getMethod(setName,String.class);
               //获取当前时间差
               String str = getDatePoor(strTime);
               //System.out.println(str);
               //给t1对象赋值
               m.invoke(objT,str);
           }catch (Exception e){
               System.out.println("时间转换异常-------------------------");
           }
        }
        System.out.println(objT);
        return objT;
    }

    //时间装换
    private String getDatePoor(String strTime) {

        SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd ");
        Date endDate = null;
        try {
            endDate = formatter.parse(strTime);
        }catch (Exception e){
            System.out.println("时间转换异常-------------------------");
        }

        long nm = 1000 * 60;
        long nh = 1000 * 60 * 60;
        long nd = 1000 * 24 * 60 * 60;
        long mh = 1000 * 24 * 60 * 60 * 30;
        long yr = 1000 * 24 * 60 * 60 * 30 * 12;

        long diff =  new Date().getTime()- endDate.getTime();

        long min = diff / nm;
        long hour = diff / nh;
        long day = diff / nd;
        long month = diff / mh;
        long year = diff / yr;

        if(min < 60 && min > 1){
            return min + "分钟前";
        }else if(hour < 24 && hour > 1){
            return hour + "小时前";
        }else if(day < 30 && day > 1){
            return day + "天前" ;
        }else if(month < 12 && month > 1){
            return month + "月前";
        }else if(year > 1){
            return year + "年前";
        }else{
            return year + "刚刚";
        }

    }
}
